﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utilities;
using CrystalDecisions.CrystalReports.Engine;

namespace ManageTicket.Report
{
    public partial class frmDeptReport : Form
    {
        #region Contructor
        public frmDeptReport(string partner)
        {
            InitializeComponent();
            PARTNER = partner;
        }
        #endregion

        #region Variable
        clsMethod4All objMethod = new clsMethod4All();
        private string PARTNER = "";
        #endregion

        #region Event
        private void frmDeptReport_Load(object sender, EventArgs e)
        {
            switch (PARTNER)
            {
                case "PROVIDER":
                    GetListProvider();
                    this.Text = "Công nợ nhà cung cấp";
                    lblPartnerId.Text = "Mã nhà cung cấp";
                    lblPartnerName.Text = "Tên nhà cung cấp";
                    break;
                case "CUSTOMER":
                    GetListCustomer();
                    this.Text = "Công nợ khách hàng";
                    lblPartnerId.Text = "Mã khách hàng";
                    lblPartnerName.Text = "Tên khách hàng";
                    break;
                case "COMMISSION":
                    GetListCustomer();
                    this.Text = "Hoa hồng";
                    lblPartnerId.Text = "Mã khách hàng";
                    lblPartnerName.Text = "Tên khách hàng";
                    break;
            }
            //default date value
            dtpDateFrom.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1, 0, 0, 0);
            dtpDateTo.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, objMethod.GetEndDayOfMonth(DateTime.Now.Month, DateTime.Now.Year), 23, 59, 59);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {

            switch (PARTNER)
            {
                case "PROVIDER":
                    DeptProvider();
                    break;
                case "CUSTOMER":
                    DeptCustomer();
                    break;
                case "COMMISSION":
                    DeptCommission();
                    break;
            }
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region Method
        private void GetListProvider()
        {
            try
            {
                Business.Provider boProvider = new Business.Provider();
                ArrayList arr = boProvider.List();
                DataTable dataTable1 = new DataTable("Provider");
                dataTable1.Columns.Add("ProviderId", typeof(String));
                dataTable1.Columns.Add("ProviderName", typeof(String));

                dataTable1.Rows.Add("ALL", "Tất cả");

                foreach (DataObjects.Provider oPro in arr)
                {
                    dataTable1.Rows.Add(new String[] { oPro.ProviderId, oPro.ProviderName });
                }
                cboPartnerId.DataSource = dataTable1;
                cboPartnerId.ValueMember = "ProviderName";
                cboPartnerId.DisplayMember = "ProviderId";
                cboPartnerId.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void GetListCustomer()
        {
            try
            {
                Business.Customer boCustomer = new Business.Customer();
                ArrayList arr = boCustomer.List();
                DataTable dataTable1 = new DataTable("Customer");
                dataTable1.Columns.Add("CustomerId", typeof(String));
                dataTable1.Columns.Add("CustomerName", typeof(String));

                dataTable1.Rows.Add("ALL", "Tất cả");

                foreach (DataObjects.Customer oCus in arr)
                {
                    dataTable1.Rows.Add(new String[] { oCus.CustomerId, oCus.CustomerName });
                }
                cboPartnerId.DataSource = dataTable1;
                cboPartnerId.ValueMember = "CustomerName";
                cboPartnerId.DisplayMember = "CustomerId";
                cboPartnerId.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Aquarius", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private string GetEmployeeName(string empId)
        {
            Business.Employee boEmployee = new Business.Employee();
            DataObjects.Employee oEmployee = boEmployee.GetObjectByPK(empId);
            if (oEmployee != null)
                return oEmployee.LastName + " " + oEmployee.FirstName;
            return "";
        }

        private string GetProviderName(string providerId)
        {
            Business.Provider boProvider = new Business.Provider();
            DataObjects.Provider oProvider = boProvider.GetObjectByPK(providerId);
            if (oProvider != null)
                return oProvider.ProviderName;
            return "";
        }

        private string GetCustomerName(string customeId)
        {
            Business.Customer boCustomer = new Business.Customer();
            DataObjects.Customer oCustomer = boCustomer.GetObjectByPK(customeId);
            if (oCustomer != null)
                return oCustomer.CustomerName;
            return "";
        }

        private string GetCodeTypeName(string Id)
        {
            Business.CodeType boCodeType = new Business.CodeType();
            DataObjects.CodeType oCodeType = boCodeType.GetObjectByPK(Id);
            if (oCodeType != null)
                return oCodeType.CodeTypeName;
            return "";
        }

        private double TinhSoTienDaTra(string voucher_no, string type)
        {
            //type : loai tien da tra
            //value :   CUR_1 --> VND
            //          CUR_2 --> USD
            try
            {
                double money = 0;

                Business.Invoice boInvoice = new Business.Invoice();
                ArrayList arr = boInvoice.GetListByVoucherNo(voucher_no);
                foreach (DataObjects.Invoice oInv in arr)
                {
                    if (oInv.KindOfVoucher == "VSTATUS_4" && oInv.Currency == type)
                        money += oInv.Money;
                }
                return money;
            }
            catch (Exception ex)
            {
                return 0;
                throw ex;
            }
        }

        private double TinhSoTienDaTraNCC(string voucher_no, string type)
        {
            //type : loai tien da tra
            //value :   CUR_1 --> VND
            //          CUR_2 --> USD
            try
            {
                double money = 0;

                Business.Invoice boInvoice = new Business.Invoice();
                ArrayList arr = boInvoice.GetListByVoucherNo(voucher_no);
                foreach (DataObjects.Invoice oInv in arr)
                {
                    if (oInv.KindOfVoucher == "VSTATUS_2" && oInv.Currency == type)
                        money += oInv.Money;
                }
                return money;
            }
            catch (Exception ex)
            {
                return 0;
                throw ex;
            }
        }

        private double TinhSoTienDaTraHH(string voucher_no, string type)
        {
            //type : loai tien da tra
            //value :   CUR_1 --> VND
            //          CUR_2 --> USD
            try
            {
                double money = 0;

                Business.Invoice boInvoice = new Business.Invoice();
                ArrayList arr = boInvoice.GetListByVoucherNo(voucher_no);
                foreach (DataObjects.Invoice oInv in arr)
                {
                    if (oInv.KindOfVoucher == "VSTATUS_5" && oInv.Currency == type)
                        money += oInv.Money;
                }
                return money;
            }
            catch (Exception ex)
            {
                return 0;
                throw ex;
            }
        }

        private void DeptProvider()
        {
            //data table Voucher
            DataTable dtVoucher = objMethod.CreateDataTable("DEPT_CUSTOMER", new string[] {"VOUCHER_NO", "DATE_OF_VOUCHER", "CONTENT", 
                "SALE_USD", "DEPT_USD", "EXCHANGE", "QUY_DOI_VND", "SALE_VND", "DEPT_VND", "BALANCE", "CUSTOMER_ID", "CUSTOMER_NAME"});

            //search
            Business.Voucher boVoucher = new Business.Voucher();
            object[] oParam = new object[14];
            oParam[0] = "";
            oParam[1] = "";
            oParam[2] = "";
            oParam[3] = "";
            oParam[4] = "";
            if (cboPartnerId.Text == "ALL")
                oParam[5] = "";
            else
                oParam[5] = cboPartnerId.Text;
            oParam[6] = "";
            DateTime dFrom;
            if (dtpDateFrom.Checked)
                dFrom = new DateTime(dtpDateFrom.Value.Year, dtpDateFrom.Value.Month, dtpDateFrom.Value.Day, 0, 0, 0);
            else
                dFrom = new DateTime(1983, 7, 24, 0, 0, 0);
            oParam[7] = dFrom;
            DateTime dTo;
            if (dtpDateTo.Checked)
                dTo = new DateTime(dtpDateTo.Value.Year, dtpDateTo.Value.Month, dtpDateTo.Value.Day, 23, 59, 59);
            else
                dTo = new DateTime(2100, 1, 1, 0, 0, 0);
            oParam[8] = dTo;
            oParam[9] = 0;
            oParam[10] = 0;
            oParam[11] = 0;
            oParam[12] = 0;
            oParam[13] = "";

            //goi ham search
            ArrayList arr = new ArrayList();
            arr = boVoucher.SearchVoucher(oParam);

            DataRow row;
            foreach (DataObjects.Voucher oVou in arr)
            {
                row = dtVoucher.NewRow();//"CUSTOMER_NAME", "DEPT_USD", "EXCHANGE", "QUY_DOI_VND", "SALE_VND", "DEPT_VND", "BALANCE"
                row["VOUCHER_NO"] = oVou.VoucherNo;
                row["DATE_OF_VOUCHER"] = oVou.DateOfVoucher.Value.ToString("dd/MM/yyyy");
                row["CUSTOMER_ID"] = oVou.ProviderId;
                row["CUSTOMER_NAME"] = GetProviderName(oVou.ProviderId);

                string content = "";
                content += "Tên khách hàng: " + oVou.CustomerNameDirect + "\r\n";
                content += "Hành trình: " + oVou.Tour + "\r\n";
                content += "Số vé: " + oVou.TicketNo + "\r\n";
                content += "Hãng vận chuyển: " + GetCodeTypeName(oVou.TransportId) + "\r\n";
                content += "Nhân viên: " + GetEmployeeName(oVou.EmployeeId) + "\r\n";
                content += "Ghi chú: " + oVou.Remark + "\r\n";
                row["CONTENT"] = content;

                double muaUSD = 0;
                double datraUSD = 0;
                double noUSD = 0;
                double tygia = 0;
                double quydoiVND = 0;
                double muaVND = 0;
                double datraVND = 0;
                double noVND = 0;
                double balance = 0;

                if (oVou.BuyCurrency == "CUR_1") //VND
                {
                    muaVND = oVou.BuySum;
                }
                else if (oVou.BuyCurrency == "CUR_2") //USD
                {
                    muaUSD = oVou.BuySum;
                }

                datraVND = TinhSoTienDaTraNCC(oVou.VoucherNo, "CUR_1");
                noVND = muaVND - datraVND;

                datraUSD = TinhSoTienDaTraNCC(oVou.VoucherNo, "CUR_2");
                noUSD = muaUSD - datraUSD;
                tygia = oVou.BuyExchange;
                quydoiVND = noUSD * tygia;

                balance = quydoiVND + noVND;

                row["SALE_USD"] = muaUSD;
                row["DEPT_USD"] = noUSD;
                row["EXCHANGE"] = tygia;
                row["QUY_DOI_VND"] = quydoiVND;
                row["SALE_VND"] = muaVND;
                row["DEPT_VND"] = noVND;
                row["BALANCE"] = balance;

                dtVoucher.Rows.Add(row);
            }

            Report.rptDeptProvider objObj = new ManageTicket.Report.rptDeptProvider();
            objObj.SetDataSource(dtVoucher);
            FormulaFieldDefinitions crFormulas;
            crFormulas = objObj.DataDefinition.FormulaFields;
            crFormulas["DateFrom"].Text = "'" + dFrom.ToString("dd/MM/yyyy").Replace("'", "''") + "'";
            crFormulas["DateTo"].Text = "'" + dTo.ToString("dd/MM/yyyy").Replace("'", "''") + "'";

            Report.frmReport objReport = new ManageTicket.Report.frmReport(objObj);
            objReport.ShowDialog();
        }

        private void DeptCustomer()
        {
            //data table Voucher
            DataTable dtVoucher = objMethod.CreateDataTable("DEPT_CUSTOMER", new string[] {"VOUCHER_NO", "DATE_OF_VOUCHER", "CONTENT", 
                "SALE_USD", "DEPT_USD", "EXCHANGE", "QUY_DOI_VND", "SALE_VND", "DEPT_VND", "BALANCE", "CUSTOMER_ID", "CUSTOMER_NAME"});

            //search
            Business.Voucher boVoucher = new Business.Voucher();
            object[] oParam = new object[14];
            oParam[0] = "";
            oParam[1] = "";
            oParam[2] = "";
            oParam[3] = "";
            oParam[4] = "";
            if (cboPartnerId.Text == "ALL")
                oParam[5] = "";
            else
                oParam[5] = cboPartnerId.Text;
            oParam[6] = "";
            DateTime dFrom;
            if (dtpDateFrom.Checked)
                dFrom = new DateTime(dtpDateFrom.Value.Year, dtpDateFrom.Value.Month, dtpDateFrom.Value.Day, 0, 0, 0);
            else
                dFrom = new DateTime(1983, 7, 24, 0, 0, 0);
            oParam[7] = dFrom;
            DateTime dTo;
            if (dtpDateTo.Checked)
                dTo = new DateTime(dtpDateTo.Value.Year, dtpDateTo.Value.Month, dtpDateTo.Value.Day, 23, 59, 59);
            else
                dTo = new DateTime(2100, 1, 1, 0, 0, 0);
            oParam[8] = dTo;
            oParam[9] = 0;
            oParam[10] = 0;
            oParam[11] = 0;
            oParam[12] = 0;
            oParam[13] = "";

            //goi ham search
            ArrayList arr = new ArrayList();
            arr = boVoucher.SearchVoucher(oParam);

            DataRow row;
            foreach (DataObjects.Voucher oVou in arr)
            {
                row = dtVoucher.NewRow();//"CUSTOMER_NAME", "DEPT_USD", "EXCHANGE", "QUY_DOI_VND", "SALE_VND", "DEPT_VND", "BALANCE"
                row["VOUCHER_NO"] = oVou.VoucherNo;
                row["DATE_OF_VOUCHER"] = oVou.DateOfVoucher.Value.ToString("dd/MM/yyyy");
                row["CUSTOMER_ID"] = oVou.CustomerId;
                row["CUSTOMER_NAME"] = GetCustomerName(oVou.CustomerId);

                string content = "";
                content += "Tên khách hàng: " + oVou.CustomerNameDirect + "\r\n";
                content += "Hành trình: " + oVou.Tour + "\r\n";
                content += "Số vé: " + oVou.TicketNo + "\r\n";
                content += "Hãng vận chuyển: " + GetCodeTypeName(oVou.TransportId) + "\r\n";
                content += "Nhân viên: " + GetEmployeeName(oVou.EmployeeId) + "\r\n";
                content += "Ghi chú: " + oVou.Remark + "\r\n";
                row["CONTENT"] = content;

                double muaUSD = 0;
                double datraUSD = 0;
                double noUSD = 0;
                double tygia = 0;
                double quydoiVND = 0;
                double muaVND = 0;
                double datraVND = 0;
                double noVND = 0;
                double balance = 0;

                if (oVou.SaleCurrency == "CUR_1") //VND
                {
                    muaVND = oVou.SaleSum;
                }
                else if (oVou.SaleCurrency == "CUR_2") //USD
                {
                    muaUSD = oVou.SaleSum;
                }

                datraVND = TinhSoTienDaTra(oVou.VoucherNo, "CUR_1");
                noVND = muaVND - datraVND;

                datraUSD = TinhSoTienDaTra(oVou.VoucherNo, "CUR_2");
                noUSD = muaUSD - datraUSD;
                tygia = oVou.SaleExchange;
                quydoiVND = noUSD * tygia;

                balance = quydoiVND + noVND;

                row["SALE_USD"] = muaUSD;
                row["DEPT_USD"] = noUSD;
                row["EXCHANGE"] = tygia;
                row["QUY_DOI_VND"] = quydoiVND;
                row["SALE_VND"] = muaVND;
                row["DEPT_VND"] = noVND;
                row["BALANCE"] = balance;

                dtVoucher.Rows.Add(row);
            }

            Report.rptDeptCustomer objObj = new ManageTicket.Report.rptDeptCustomer();
            objObj.SetDataSource(dtVoucher);
            FormulaFieldDefinitions crFormulas;
            crFormulas = objObj.DataDefinition.FormulaFields;
            crFormulas["DateFrom"].Text = "'" + dFrom.ToString("dd/MM/yyyy").Replace("'", "''") + "'";
            crFormulas["DateTo"].Text = "'" + dTo.ToString("dd/MM/yyyy").Replace("'", "''") + "'";

            Report.frmReport objReport = new ManageTicket.Report.frmReport(objObj);
            objReport.ShowDialog();
        }

        private void DeptCommission()
        {
            //data table Voucher
            DataTable dtVoucher = objMethod.CreateDataTable("DEPT_CUSTOMER", new string[] {"VOUCHER_NO", "DATE_OF_VOUCHER", "CONTENT", 
                "COMMISSION", "SUM_PAID", "DEPT", "CUSTOMER_ID", "CUSTOMER_NAME"});

            //search
            Business.Voucher boVoucher = new Business.Voucher();
            object[] oParam = new object[14];
            oParam[0] = "";
            oParam[1] = "";
            oParam[2] = "";
            oParam[3] = "";
            oParam[4] = "";
            if (cboPartnerId.Text == "ALL")
                oParam[5] = "";
            else
                oParam[5] = cboPartnerId.Text;
            oParam[6] = "";
            DateTime dFrom;
            if (dtpDateFrom.Checked)
                dFrom = new DateTime(dtpDateFrom.Value.Year, dtpDateFrom.Value.Month, dtpDateFrom.Value.Day, 0, 0, 0);
            else
                dFrom = new DateTime(1983, 7, 24, 0, 0, 0);
            oParam[7] = dFrom;
            DateTime dTo;
            if (dtpDateTo.Checked)
                dTo = new DateTime(dtpDateTo.Value.Year, dtpDateTo.Value.Month, dtpDateTo.Value.Day, 23, 59, 59);
            else
                dTo = new DateTime(2100, 1, 1, 0, 0, 0);
            oParam[8] = dTo;
            oParam[9] = 0;
            oParam[10] = 0;
            oParam[11] = 0;
            oParam[12] = 0;
            oParam[13] = "";

            //goi ham search
            ArrayList arr = new ArrayList();
            arr = boVoucher.SearchVoucher(oParam);

            DataRow row;
            foreach (DataObjects.Voucher oVou in arr)
            {
                row = dtVoucher.NewRow();
                row["VOUCHER_NO"] = oVou.VoucherNo;
                row["DATE_OF_VOUCHER"] = oVou.DateOfVoucher.Value.ToString("dd/MM/yyyy");
                row["CUSTOMER_ID"] = oVou.CustomerId;
                row["CUSTOMER_NAME"] = GetCustomerName(oVou.CustomerId);

                string content = "";
                content += "Tên khách hàng: " + oVou.CustomerNameDirect + "\r\n";
                content += "Hành trình: " + oVou.Tour + "\r\n";
                content += "Số vé: " + oVou.TicketNo + "\r\n";
                content += "Hãng vận chuyển: " + GetCodeTypeName(oVou.TransportId) + "\r\n";
                content += "Nhân viên: " + GetEmployeeName(oVou.EmployeeId) + "\r\n";
                content += "Ghi chú: " + oVou.Remark + "\r\n";
                row["CONTENT"] = content;

                double hoahong = 0;
                double datra = 0;
                double no = 0;

                hoahong = oVou.SaleCommission;
                datra = TinhSoTienDaTraHH(oVou.VoucherNo, "CUR_1");
                no = hoahong - datra;

                row["COMMISSION"] = hoahong;
                row["SUM_PAID"] = datra;
                row["DEPT"] = no;

                dtVoucher.Rows.Add(row);
            }

            Report.rptDeptCommission objObj = new ManageTicket.Report.rptDeptCommission();
            objObj.SetDataSource(dtVoucher);
            FormulaFieldDefinitions crFormulas;
            crFormulas = objObj.DataDefinition.FormulaFields;
            crFormulas["DateFrom"].Text = "'" + dFrom.ToString("dd/MM/yyyy").Replace("'", "''") + "'";
            crFormulas["DateTo"].Text = "'" + dTo.ToString("dd/MM/yyyy").Replace("'", "''") + "'";

            Report.frmReport objReport = new ManageTicket.Report.frmReport(objObj);
            objReport.ShowDialog();
        }
        #endregion
 
    }
}
